package com.aaa.member.config;

import com.aaa.member.util.MyRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: YuXin
 * @Date : Created in 2020/11/13 13:56
 * @ClassName : ShiroConfig
 * @Description : shiro
 * @Package_Name : com.aaa.sso.config
 */
@Configuration
//@ComponentScan("com.aaa.common.controller")
public class ShiroConfig {
    /**
     * 初始化shiro过滤工厂类
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        //设置未认证的跳转地址
        //shiroFilterFactoryBean.setLoginUrl("/html/login.html");
        //设置安全管理器
        //设置未授权跳转页面
        //shiroFilterFactoryBean.setUnauthorizedUrl("");
        //设置成功后跳转页面
        //shiroFilterFactoryBean.setSuccessUrl();

        //因为要把 /**= authc 放在最下面，如果使用HashMap 就无序了
        Map<String, String> filterMap = new LinkedHashMap<>();
        //需要放开的请求  anon  anonymous 匿名的缩写
        //静态资源
        //filterMap.put("/css/**", "anon");
        //filterMap.put("/imgs/**", "anon");
        //filterMap.put("/js/**", "anon");
        //模块相关
        //filterMap.put("/mrs/news/**", "anon");
        //filterMap.put("/crs/car/**", "anon");
        //filterMap.put("/sso/ssoTokenController/userLogin", "anon");

        filterMap.put("/**", "anon");

        //登录相关
        //filterMap.put("/html/login.html", "anon");
        //filterMap.put("/user/login", "anon");
        //注销
        //filterMap.put("/logout", "logout");
        //实际做项目过程中，放开的更多
        //除了放开的全部拦截
        //filterMap.put("/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

    /**
     * 安全管理器
     */
    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm());
        return securityManager;
    }

    /**
     * 安全数据连接桥梁  实例化realm
     */
    @Bean
    public MyRealm myRealm() {
        MyRealm myRealm = new MyRealm();
        //设置校验匹配器
        myRealm.setCredentialsMatcher(credentialsMatcher());
        return myRealm;
    }

    /**
     * 加密算法以及类型设置
     */
    @Bean
    public CredentialsMatcher credentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密方式/加密算法
        hashedCredentialsMatcher.setHashAlgorithmName("SHA-512");
        //设置哈希次数
        hashedCredentialsMatcher.setHashIterations(1024);
        return hashedCredentialsMatcher;
    }
}
